02) Using R from VS Code

Published

Tue, 30 of April, 2024

Modified

Tue, 30 of April, 2024

Caution

Web page construction in progress…

(as of 25/5/2022) # Reference

Install VS Code

Install R ext on VS Code

Install radian ext on

One of the first suggestions is to use the radian terminal. That’s not a VS Code extension but an application written in Python—which means your system needs Python installed in order for radian to run. I already have Python and the conda package manager installed on my Mac, so I used the following installation command for radian:

See the How to set up VS Code for R video tutorial for easy instructions on how to install Python for use with R and RStudio.

NEW Python version

  • Version: Python 3.9.13 for macOS 10.9 or later
  • Interpreter path: /usr/local/bin/python3

Gotcha!!!

python3 --version
# Python 3.9.13

Python

3 ways to work with python

— 1) Launch Idle shell + execute

(it comes installed with the new python3)

— 2) Execute program in shell

python3 Python3_9_test.py  
# it prompts for name etc... 

— 3) Execute program in VS code

Just look for the arrow from the top of the .py file


install radian [again!!!]

pip3 install -U radian
# then as it suggests
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -m pip install --upgrade pip

Check

radian --version

radian version: 0.6.3 r executable: /Library/Frameworks/R.framework/Resources/bin/R r version: 4.1.0 python executable: /Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python version: 3.9.13

Tweak Visual Studio Code settings for R

The easiest way to change a VS Code setting is to open up settings in the user interface. Another other option is editing the settings JSON file.

Path to open settings.json

  • [from Finder] Go to /Users/luisamimmi/Library/Application Support/Code/User/settings.json
  • [from VS code] Open by ctr+shift+P and then “Preferences: Open Settings (json)”

Open in finder

From shell

open -a Finder /Library/Frameworks/R.framework/Resources

modify R Extension settings

I need to specify the path to R R > Rpath: mac

# here are the commands to figure it out 
R 
R.home("bin")
# [1] "/Library/Frameworks/R.framework/Resources/bin"

To color the console I need to have radian path in R settings –> R> Rterm:MAC
R > Rterm: Mac "r.rterm.mac": "/Library/Frameworks/Python.framework/Versions/3.9/bin/radian"

Sending R code to terminal

  • Ctrl + Shift + P and choose “R: Create R terminal” command
  • oppure lancio R interactive dalla tendina in Terminal

Path to keybindings.json

~/Library/Application Support/Code/User/keybindings.json

Info on Keybinding

https://code.visualstudio.com/docs/getstarted/keybindings

F&$%ing lintr in VSCode

https://github.com/r-lib/lintr#continuous-integration

You can tone it down by creating your own `~/.lintr/ file like so

linters: with_defaults(
  line_length_linter = NULL,
  open_curly_linter = NULL, 
  commented_code_linter = NULL,
  trailing_whitespace_linter = NULL)